//
//  DestinationDetailViewController.m
//  QYer
//
//  Created by lanou on 16/8/10.
//  Copyright © 2016年 CaiZhuYu. All rights reserved.
//

#import "DestinationDetailViewController.h"
#import "DestinationHeaderView.h"
#import "CarouselView.h"
#import "DetailModel.h"
#import "FirstTableViewCell.h"
#import "SecondTableViewCell.h"
#import "New_DiscountModel.h"
#import "TipModel.h"
#import "TipViewController.h"
#import "Hot_CityDetailViewController.h"
#import "New_DiscountDetailViewController.h"
@interface DestinationDetailViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) DestinationHeaderView *detailHeaderView;
@property (nonatomic, strong) NSMutableArray *scrollImageArray;
@property (nonatomic, strong) NSMutableArray *scrollImageArray1;
@property (nonatomic, strong) NSMutableArray *hotCityArray;
@property (nonatomic, strong) NSMutableArray *OtherCityArray;
@property (nonatomic, strong) NSString *contentString;
@property (nonatomic, strong) NSString *contentString1;
@property (nonatomic, strong) NSMutableArray *discountArray;
@property (nonatomic, strong) NSMutableArray *discountArray1;
@property (nonatomic, strong) NSMutableArray *tipArray;
@property (nonatomic, strong) NSMutableArray *urlArray;


@end

@implementation DestinationDetailViewController


- (NSMutableArray *)scrollImageArray
{
    if (!_scrollImageArray)
    {
        _scrollImageArray = [[NSMutableArray alloc] init];
    }
    return _scrollImageArray;
}


- (NSMutableArray *)scrollImageArray1
{
    if (!_scrollImageArray1)
    {
        _scrollImageArray1 = [[NSMutableArray alloc] init];
    }
    return _scrollImageArray1;
}


- (NSMutableArray *)hotCityArray
{
    if(!_hotCityArray)
    {
        _hotCityArray = [[NSMutableArray alloc] init];
    }
    return _hotCityArray;
}


- (NSMutableArray *)OtherCityArray
{
    if (!_OtherCityArray)
    {
        _OtherCityArray = [[NSMutableArray alloc] init];
    }
    return _OtherCityArray;
}


- (NSMutableArray *)discountArray
{
    if (!_discountArray)
    {
        _discountArray = [[NSMutableArray alloc] init];
    }
    return _discountArray;
}


- (NSMutableArray *)discountArray1
{
    if (!_discountArray1)
    {
        _discountArray1 = [[NSMutableArray alloc] init];
    }
    return _discountArray1;
}


- (NSMutableArray *)tipArray
{
    if (!_tipArray)
    {
        _tipArray = [[NSMutableArray alloc] init];
    }
    return _tipArray;
}


- (NSMutableArray *)urlArray
{
    if (!_urlArray)
    {
        _urlArray = [[NSMutableArray alloc] init];
    }
    return _urlArray;
}





- (void)createHeaderView
{
    self.detailHeaderView = [DestinationHeaderView destinationHeaderView];
    
    self.detailHeaderView.frame = CGRectMake(0, 0, self.view.frame.size.width, 300);

    [self.view addSubview:self.detailHeaderView];
}


- (void)dataAnalysis
{
    if ([self.ID  isEqual: @"62"])
    {
      
    }
    else
    {
        
        [NetWorkTool requstWithType:GET URLString:[NSString stringWithFormat:kDestinationDetailUrl,self.ID] parameter:nil callBack:^(NSData *data, NSError *error)
         {
             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
             
             // 获取轮播图的图片
             for (NSString *image in dic[@"data"][@"photos"])
             {
                 [self.scrollImageArray addObject:image];
             }
             
             // 获取头视图下的文字
             self.contentString = dic[@"data"][@"entryCont"];
             
             // 获取热门城市
             for (NSDictionary *dic1 in dic[@"data"][@"hot_city"])
             {
                 DetailModel *model = [[DetailModel alloc] init];
                 [model setValuesForKeysWithDictionary:dic1];
                 [self.hotCityArray addObject:model];
                 
             }
             
             
             // 获取超值自由行的数据
             for (NSDictionary *dic2 in dic[@"data"][@"new_discount"])
             {
                 New_DiscountModel *model = [[New_DiscountModel alloc] init];
                 [model setValuesForKeysWithDictionary:dic2];
                 [self.discountArray addObject:model];
             }
             
             
             
             
             
             dispatch_async(dispatch_get_main_queue(), ^{
                 
                 CarouselView *Carousel = [[CarouselView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 220) imageUrls:self.scrollImageArray];
                 
                 [self.detailHeaderView.scrollView addSubview:Carousel];
                 
                 self.tableView.tableHeaderView = self.detailHeaderView;
                 
                 
                 self.detailHeaderView.contentLabel.text = self.contentString;
                 
                 [self.tableView reloadData];
             });
             
             
         }];
        
        
        // 获取轮播图中的tip数据
        [NetWorkTool requstWithType:GET URLString:[NSString stringWithFormat:kTipUrl, self.ID] parameter:nil callBack:^(NSData *data, NSError *error)
         {
             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
             
             for (NSDictionary *dic1 in dic[@"data"])
             {
                 TipModel *model = [[TipModel alloc] init];
                 [model setValuesForKeysWithDictionary:dic1];
                 NSString *tipString = model.title;
                 [self.tipArray addObject:tipString];
                 NSString *urlString = model.appview_url;
                 [self.urlArray addObject:urlString];
             }
             
         }];
        
        
        
        // 获取其他国家详细数据
        [NetWorkTool requstWithType:GET URLString:[NSString stringWithFormat:kOtherCountryUrl, self.ID] parameter:nil callBack:^(NSData *data, NSError *error)
        {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            // 获取轮播图的图片
            for (NSString *image in dic[@"data"][@"photos"])
            {
                [self.scrollImageArray1 addObject:image];
            }     
            
            // 获取头视图下的文字
            self.contentString1 = dic[@"data"][@"entryCont"];
            
            // 获取热门城市
            for (NSDictionary *dic1 in dic[@"data"][@"hot_city"])
            {
                DetailModel *model = [[DetailModel alloc] init];
                [model setValuesForKeysWithDictionary:dic1];
                [self.OtherCityArray addObject:model];
                
            }
            
            
            // 获取超值自由行的数据
            for (NSDictionary *dic2 in dic[@"data"][@"new_discount"])
            {
                New_DiscountModel *model = [[New_DiscountModel alloc] init];
                [model setValuesForKeysWithDictionary:dic2];
                [self.discountArray1 addObject:model];
            }

        }];
        
    }

}
    


- (void)getData:(NSInteger)tag
{
    [NetWorkTool requstWithType:GET URLString:[NSString stringWithFormat:kHotCountryDetailUrl, self.ID] parameter:nil callBack:^(NSData *data, NSError *error)
     {
         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         
         
         // 获取热门城市
         NSDictionary *dic1 = dic[@"data"][@"hot_city"][tag - 101];
    
         DetailModel *model = [[DetailModel alloc] init];
         [model setValuesForKeysWithDictionary:dic1];
         [self.OtherCityArray addObject:model];
         
         
         
         dispatch_async(dispatch_get_main_queue(), ^{
             [self.tableView reloadData];
         });
         
     }];
    
    

}













- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"FirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"firstCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"SecondTableViewCell" bundle:nil] forCellReuseIdentifier:@"secondCell"];
    [self.view addSubview:_tableView];
}












- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self createHeaderView];
    
    [self dataAnalysis];
    
    [self createTableView];
    
    [self.detailHeaderView.tipButton addTarget:self action:@selector(clickAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    FirstTableViewCell *cell = [[FirstTableViewCell alloc] init];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    cell.imageV1.tag = 101;
    cell.imageV2.tag = 102;
    cell.imageV3.tag = 103;
    cell.imageV4.tag = 104;
    
    cell.imageV1.userInteractionEnabled = YES;
    cell.imageV2.userInteractionEnabled = YES;
    cell.imageV3.userInteractionEnabled = YES;
    cell.imageV4.userInteractionEnabled = YES;
    
    [cell.imageV1 addGestureRecognizer:tap];
    [cell.imageV2 addGestureRecognizer:tap];
    [cell.imageV3 addGestureRecognizer:tap];
    [cell.imageV4 addGestureRecognizer:tap];

}



// 香港:50   澳门:51   台湾:11186   台北:52
- (void)tapAction:(UIImageView *)imageV
{
    switch (imageV.tag)
    {
        case 101:
            [self getData:50];
            NSLog(@"%@", self.OtherCityArray[0]);
            break;
        case 102:
            [self getData:51];
        case 103:
            [self getData:11186];
        case 104:
            [self getData:52];
        default:
            break;
    }
}








- (void)clickAction
{
    TipViewController *tipVC = [[TipViewController alloc] init];
    tipVC.titleArray = self.tipArray;
    tipVC.urlArray = self.urlArray;
    NSLog(@"%@", tipVC.titleArray.firstObject);
    NSLog(@"%@", tipVC.urlArray.firstObject);
    [self.navigationController pushViewController:tipVC animated:YES];
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else
    {
        return 5;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"firstCell" forIndexPath:indexPath];
        
        if (self.hotCityArray.count > 0)
        {
            if (self.hotCityArray.count < 2)
            {
                DetailModel *model = self.hotCityArray[0];
                
                for (NSInteger i = 0; i < 2; i++)
                {
                    [cell.imageV1 sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
                }
                
                cell.name1.text = model.cnname;
                
                cell.ename1.text = model.enname;
            }
            else if (self.hotCityArray.count < 3)
            {
                DetailModel *model1 = self.hotCityArray[0];
                DetailModel *model2 = self.hotCityArray[1];
                
                NSArray *photoArray = @[model1.photo,model2.photo];
                
                NSArray *imageArray = @[cell.imageV1,cell.imageV2];
                
                
                for (NSInteger i = 0; i < 2; i++)
                {
                    [imageArray[i] sd_setImageWithURL:[NSURL URLWithString:photoArray[i]] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
                }
                
                
                cell.name1.text = model1.cnname;
                cell.name2.text = model2.cnname;
                
                cell.ename1.text = model1.enname;
                cell.ename2.text = model2.enname;

            }
            else
            {
                DetailModel *model1 = self.hotCityArray[0];
                DetailModel *model2 = self.hotCityArray[1];
                DetailModel *model3 = self.hotCityArray[2];
                DetailModel *model4 = self.hotCityArray[3];
                
                
                NSArray *photoArray = @[model1.photo,model2.photo,model3.photo,model4.photo];
                
                NSArray *imageArray = @[cell.imageV1,cell.imageV2,cell.imageV3,cell.imageV4];
                
                
                
                for (NSInteger i = 0; i < 4; i++)
                {
                    [imageArray[i] sd_setImageWithURL:[NSURL URLWithString:photoArray[i]] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
                }
                
                
                cell.name1.text = model1.cnname;
                cell.name2.text = model2.cnname;
                cell.name3.text = model3.cnname;
                cell.name4.text = model4.cnname;
                
                cell.ename1.text = model1.enname;
                cell.ename2.text = model2.enname;
                cell.ename3.text = model3.enname;
                cell.ename4.text = model4.enname;
            }
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            
            cell.imageV1.tag = 101;
            cell.imageV2.tag = 102;
            cell.imageV3.tag = 103;
            cell.imageV4.tag = 104;
            
            cell.imageV1.userInteractionEnabled = YES;
            cell.imageV2.userInteractionEnabled = YES;
            cell.imageV3.userInteractionEnabled = YES;
            cell.imageV4.userInteractionEnabled = YES;
            
            [cell.imageV1 addGestureRecognizer:tap];
            [cell.imageV2 addGestureRecognizer:tap];
            [cell.imageV3 addGestureRecognizer:tap];
            [cell.imageV4 addGestureRecognizer:tap];

            
        }
        
        return cell;
    }
    else
    {
        SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondCell" forIndexPath:indexPath];
        
        if (self.discountArray.count > 0)
        {
            New_DiscountModel *model = self.discountArray[indexPath.row];
            
            [cell setValueWithModel:model];
        }
        
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 300;
    }
    return 120;
}


//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    if (section == 0)
//    {
//        return @"热门城市";
//    }
//    return @"超值自由行";
//}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    
    if (section == 1)
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 150, 30)];
        label.text = @"超值自由行";
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(290, 15, kScreenWidth - 290 - 15, 15)];
        
//        button.backgroundColor = [UIColor redColor];
        [button setTitle:@"查看全部" forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        [button.titleLabel setFont:[UIFont systemFontOfSize:15]];

        [view addSubview:button];
        [view addSubview:label];
        
    }
    else
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 150, 30)];
        label.text = @"热门城市";
        [view addSubview:label];
    }
    return view;
}






- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        
    }
    else if (indexPath.section == 1)
    {
        New_DiscountDetailViewController *discountVC = [[New_DiscountDetailViewController alloc] init];
        New_DiscountModel *model = self.discountArray1[indexPath.row];
        discountVC.myID = model.myID;
        [self presentViewController:discountVC animated:YES completion:nil];
        NSLog(@"%@", model.myID);
    }
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
